package com.mmall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.mmall.common.ServerResponse;
import com.mmall.dao.ShippingMapper;
import com.mmall.pojo.Shipping;
import com.mmall.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @CLASS ShippingServiceimpl
 * @DESCRIPTION TODO
 * @AUTHor tang
 * @Date 2019-08-16 14:32
 * @VERSION 1.0
 **/
@Service("iShippingService")
public class ShippingServiceimpl implements IShippingService {

    @Autowired
    ShippingMapper shippingMapper;

    public ServerResponse addUpdate(Integer userId, Shipping shipping){
        if (shipping!=null){
            // 新增
            if (shipping.getId()==null){
                shipping.setUserId(userId);
                int count = shippingMapper.insert(shipping);
                if (count>0){
                    Map res = Maps.newHashMap();
                    res.put("shippingId",shipping.getId());
                    return ServerResponse.createBySuccess("新建地址成功",res);
                }
            // 修改
            }else {
                shipping.setUserId(userId);
                int count = shippingMapper.updateShippingUserId(shipping);
                if (count>0){
                    return ServerResponse.createBySuccessMessage("更新地址成功");
                }
            }
        }
        return ServerResponse.createByErrorMessage("新增修改商品失败");
    }

    public ServerResponse<PageInfo> list(Integer userId, int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Shipping> shippingList = shippingMapper.selectShippingList(userId);
        if (shippingList!=null){
            PageInfo pageInfo = new PageInfo(shippingList);
            pageInfo.setList(shippingList);
            return ServerResponse.createBySuccess(pageInfo);
        }
        return ServerResponse.createByErrorMessage("查询商品失败");
    }

    public ServerResponse delete(Integer shipping,Integer userId){
        int count = shippingMapper.deleteByShippingUserId(shipping,userId);
        if (count>0){
            return ServerResponse.createBySuccessMessage("删除商品成功");
        }
        return ServerResponse.createByErrorMessage("删除商品失败");
    }

    public ServerResponse<Shipping> detail(Integer shippingId){
        Shipping shipping = shippingMapper.selectByPrimaryKey(shippingId);
        if (shipping!=null){
            return ServerResponse.createBySuccess(shipping);
        }
        return ServerResponse.createByErrorMessage("查看商品详情失败");
    }
}
